package com.superteam.zzl.Des;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.google.gson.Gson;
import com.superteam.zzl.activity.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/8.
 */
public class Second extends AppCompatActivity implements ViewPager.OnTouchListener{
    List<String>stringList;
    private List<Object> list;
    private List<Integer> list1=new ArrayList<>();

   private int[] imgs={R.drawable.home_icon_must, R.drawable.home_icon_restaurant,R.drawable.home_icon_sight,
   R.drawable.home_icon_entertainment,R.drawable.index_translate,R.drawable.index_weather,
   R.drawable.index_change,R.drawable.index_clock,R.drawable.home_icon_shop,
   R.drawable.home_icon_hotel,R.drawable.home_icon_traffic,R.drawable.home_icon_path,
   R.drawable.home_icon_intro,R.drawable.home_icon_other,R.drawable.home_icon_favriote};
    private TextView tv;
    private String shuju1;
    private String shuju2;
    private MyPutongAdapter adapter;
    private ListView listView;
    private View headView;
    private ConvenientBanner mCb;
    private ImageCarousel imageCarousel;
    private ArrayList<Bitmap> bitmaps;
    private RecommHeadPager1Adapter hadapter1;
    private ViewPager vp,vp2;
    private GridView gv,gv1;
    private ArrayAdapter arrayAdapter;
    private GvAdapter gvAdapter,gvAdapter2;
    private List<View> viewList;
    private LoadPageAdapter lpa;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        getSupportActionBar().hide();

        Intent intent=getIntent();
        shuju1=intent.getStringExtra("shuju1");
        stringList=new ArrayList<>();
        shuju2=shuju1.replaceAll("board/board_index","client/ad_list");
        headView=View.inflate(this,R.layout.head,null);

        vp=(ViewPager)headView.findViewById(R.id.head_vp1);
        vp2=(ViewPager)headView.findViewById(R.id.vp2);

        vp.setOnTouchListener(this);

        gv=(GridView)headView.findViewById(R.id.vpgv);
        gv1=(GridView)headView.findViewById(R.id.vpgv);

        bitmaps = new ArrayList<>();
       // arrayAdapter=new ArrayAdapter(getApplicationContext(),R.layout.gvitem,imgs);

       // gv.setAdapter(arrayAdapter);
viewList=new ArrayList<>();

        viewList.add(gv);

        viewList.add(gv1);
        Log.e("vvvv",viewList.get(0).toString());

        lpa=new LoadPageAdapter(viewList);

      //  mCb=(ConvenientBanner)headView.findViewById(R.id.cb);
        vp2.setAdapter(lpa);
        ImageLoad();
        init();

    }

    private void ImageLoad() {


        new JsonAsyncTask(this, new JsonAsyncTask.CallBack() {
            @Override
            public void sendData(String json) {
              Addboo addboo=new Gson().fromJson(json,Addboo.class);
                for (int i = 0; i < addboo.data.home_banner.size(); i++) {
                    stringList.add(addboo.data.home_banner.get(i).image_url);
                    hadapter1=new RecommHeadPager1Adapter(getApplicationContext(),stringList);
                    hadapter1.notifyDataSetChanged();
                    vp.setAdapter(hadapter1);
                    hadapter1.notifyDataSetChanged();
                }

            }
        }).execute(shuju2);




}



    private void init() {
        tv=(TextView)findViewById(R.id.text);
        list=new ArrayList<>();
        adapter=new MyPutongAdapter(this,list);
        listView=(ListView)findViewById(R.id.lv);
        listView.addHeaderView(headView);
        listView.setAdapter(adapter);

        list1.add(R.drawable.home_icon_must);
        list1.add(R.drawable.home_icon_restaurant);
        list1.add(R.drawable.home_icon_sight);
        list1.add(R.drawable.home_icon_entertainment);
        list1.add(R.drawable.index_translate);
        list1.add(R.drawable.index_weather);
        list1.add(R.drawable.index_change);
        list1.add(R.drawable.index_clock);
        list1.add(R.drawable.home_icon_shop);
        list1.add(R.drawable.home_icon_hotel);
        list1.add(R.drawable.home_icon_traffic);
        list1.add(R.drawable.home_icon_path);
        list1.add(R.drawable.home_icon_intro);
        list1.add(R.drawable.home_icon_other);
        list1.add(R.drawable.home_icon_favriote);


        gvAdapter2=new GvAdapter(getApplicationContext(),list1.subList(0,8));





        gvAdapter2.notifyDataSetChanged();




        gv1.setAdapter(gvAdapter2);
        loadData();
    }

    private void loadData() {


        new JsonAsyncTask(this, new JsonAsyncTask.CallBack() {
            @Override
            public void sendData(String json) {
               // tv.setText(json);
              Putong2 putong2=new Gson().fromJson(json,Putong2.class);
        //        list.add(putong2.data);

                list.add(putong2.data.index_trends);
                list.addAll(putong2.data.cms_list);
                list.add(putong2.data.recommend_pin_list);


                adapter.notifyDataSetChanged();
            }
        }).execute(shuju1);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //flage = 0 ;
                break ;
            case MotionEvent.ACTION_MOVE:
             //   flage = 1 ;
                break ;
            case  MotionEvent.ACTION_UP :
//                if (flage == 0) {
//                    int item = viewPager.getCurrentItem();
//                    if (item == 0) {
//                        Intent intent = new Intent(sa, NoNetWork.class);
//                        sa.startActivity(intent);
//                    } else if (item == 1) {
//                        Intent intent = new Intent(sa, NoNetWork.class);
//                        sa.startActivity(intent);
//                    } else if (item == 2) {
//                        Intent intent = new Intent(sa, NoNetWork.class);
//                        sa.startActivity(intent);
//                    }else if (item == 3) {
//                        Intent intent = new Intent(sa, NoNetWork.class);
//                        sa.startActivity(intent);
//                    }
//                }
                int item = vp.getCurrentItem();
                if (item==0){
                    Intent intent=new Intent(this,HeheActivity.class);
                   // intent.putExtra("shuju",tuijianlist.get(position));
                    startActivity(intent);
                }
                break ;


        }
        return false;
    }
}











//
//    public void hehe(Context context, String data) {
//        //   [html] view plain copy 在CODE上查看代码片派生到我的代码片
//        //glide加载出图片，data是传过来的图片地址，
//        Log.e("shuju",data);
//        Glide.with(context).load(data).asBitmap().into(new SimpleTarget<Bitmap>() {
//            @Override
//            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                bitmaps.add(resource);
//                new ImageCarousel(mCb,bitmaps);
//            }
//        });
//    }
//
